# coding: utf-8
from __future__ import absolute_import
from urllib.parse import quote
from celery.schedules import crontab
from app.config import server_config

# broker 配置 使用redis作为消息中间件
PASSWORD = quote(server_config.get('redis_password'))
BROKER_REDIS_URL = server_config.get('redis_url') + '1'
BROKER_URL = BROKER_REDIS_URL.format(PASSWORD)

CELERY_IMPORTS = ('app.cron.tasks')
# broker_pool_limit = 0
BROKER_POOL_LIMIT = 0
# BACKEND配置，使用redis
CELERY_REDIS_URL = server_config.get('redis_url') + '2'
CELERY_RESULT_BACKEND = CELERY_REDIS_URL.format(PASSWORD)

CELERY_TIMEZONE = 'Asia/Shanghai'
timezone = 'Asia/Shanghai'
USE_TZ = False
CELERY_ENABLE_UTC = False
DJANGO_CELERY_BEAT_TZ_AWARE = False
CELERY_ACCEPT_CONTENT = ['json']
# accept_content = ['json']
CELERY_TASK_SERIALIZER = 'json'
# task_serializer = 'json'
CELERY_RESULT_SERIALIZER = 'json'
# result_serializer = 'json'
# 每个worker最多执行6个任务被销毁，可以防止内存泄漏
CELERYD_MAX_TASKS_PER_CHILD = 6
# worker_max_tasks_per_child = 6

# CELERYBEAT_SCHEDULER = 'django_celery_beat.schedulers.DatabaseScheduler'
# beat_schedule = 'django_celery_beat.schedulers.DatabaseScheduler'

# 定时任务
CELERYBEAT_SCHEDULE = {
    'sync_server_cron': {
        "task": "app.cron.tasks.sync_server_cron",
        "schedule": crontab(minute=10, hour=0),
    },
    'reverse_server_cron': {
        "task": "app.cron.tasks.reverse_server_cron",
        "schedule": crontab(minute=10, hour=3),
    }
}
